%%%====================================================================
%%% File    : pipe_filter.erl
%%% Author  :  <vjache>
%%% Description: This is a simpliest pipe implementation. It may be 
%%%				 considered as a simple reference implementation of 
%%%				 a pipe callback module, and could be used for debug 
%%%				 purposes.
%%%
%%% Created : 24.03.2009
%%%====================================================================

-module(pipe_filter).

%%
%% Include files
%%
-include("../include/pipe.hrl").
%%
%% Exported Functions
%%
-export([
		 new/2,
		 open/1,
		 push/2,
		 flush/1,
		 close/1,
		 test/1]).

%%====================================================================
%% API Functions
%%====================================================================

new(PredFun,NextPipe) ->
	pipe:pipe_from_module(?MODULE, PredFun, NextPipe).

open({PredFun,NextPipe}) -> %%PipeState
    {PredFun,pipe:open(NextPipe)}.
push({PredFun,NextPipeState},?EOF) ->
	{PredFun,pipe:push(NextPipeState,?EOF)};
push({PredFun,NextPipeState},Item) ->
	case PredFun(Item) of
		true ->
			{PredFun,pipe:push(NextPipeState,Item)};
		false ->
			{PredFun,NextPipeState}
	end.
flush({PredFun,NextPipeState}) ->
	{PredFun,pipe:flush(NextPipeState)}.
close({_PredFun,NextPipeState}) ->
	ok=pipe:close(NextPipeState).

%%====================================================================
%% Test Functions
%%====================================================================

test(L) ->
	S=pipe:open(new(fun(Item)->
							Item < 10 
	end ,pipe_console:new())),
	{IterState,PipeState}=pipe:push_each(S, iter:from_list(L)),
	iter:close(IterState),
	pipe:close(PipeState).


%%====================================================================
%% Local Functions
%%====================================================================

